global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/Min_wage_conversion.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {

set more off
qui do ${code_dir}/config/labeling_indepvars_function.do



*Use the hourly minimum wage and convert it into USD in 1995 and deflate it 
*by ppi. 
use ${mow_data_proc}\wage_combined_final_with_min_wage.dta, clear


*Now we want to convert the minimum wage. 
*For that we need the exchange rate in 1995 and the ppi of manufacturing in 1995. 
merge 1:1 year country code using ${mow_data_proc}/exchange_rate_ppi.dta
drop if _merge==2
drop _merge

*Divide the houerly min wage by ppi
gen Hourly_pi_95=Hourly/VA_P_95*100

*Converte to USD using constant 1995 exchange rate

*First fix Lithuania. 
*From 2002 till 2009 multiply the min wage by the latest conversion of EURO to Lithuanian litas
*Before: use USD EURO exchange rate to put it into Dollars and divide by 4 (took it from the UNSTATS database where we have the exchange rates from - from the row with Lithuania) 
*Then we have local lithuanian currency. 
replace Hourly = Hourly*3.4528 if country=="ltu" & year >2001
replace Hourly = (Hourly/1.16)*4 if country=="ltu" & year <2002



* Divide by the exchange rate in 1995.
* (Note: Exchange rate is already construced in the load_data_additional_data.do such that it is the conversion of the local 
* currancy to USD till the Euro is introduced and from then one the exchange rate is the conversion of 
* Euro to USD.)
foreach var in Hourly { 
    gen `var'_pi_USD_95=`var'_pi_95/value_1995

    * And then we have to fix the conversion for the countries that joined the Euro later 
    * than 1995 (which is all EU countries). 
    replace `var'_pi_USD_95=`var'_pi_USD_95*239.64 if country=="svn" 
    replace `var'_pi_USD_95=`var'_pi_USD_95*30.1260 if country=="svk" 
    replace `var'_pi_USD_95=`var'_pi_USD_95*0.429300 if country=="mlt" 
    replace `var'_pi_USD_95=`var'_pi_USD_95*0.585274 if country=="cyp" 
    replace `var'_pi_USD_95=`var'_pi_USD_95*15.6466 if country=="est" 

    replace `var'_pi_USD_95=`var'_pi_USD_95*166.386 if country=="esp" 
    replace `var'_pi_USD_95=`var'_pi_USD_95*13.7603 if country=="aut" 
    replace `var'_pi_USD_95=`var'_pi_USD_95*40.3399 if country=="bel" 
    replace `var'_pi_USD_95=`var'_pi_USD_95*5.94573 if country=="fin" 
    replace `var'_pi_USD_95=`var'_pi_USD_95*6.55957 if country=="fra" 
    replace `var'_pi_USD_95=`var'_pi_USD_95*1.95583 if country=="deu" 

    replace `var'_pi_USD_95=`var'_pi_USD_95*340.750 if country=="grc" 
    replace `var'_pi_USD_95=`var'_pi_USD_95*0.787564 if country=="irl" 
    replace `var'_pi_USD_95=`var'_pi_USD_95*1936.27 if country=="ita" 
    replace `var'_pi_USD_95=`var'_pi_USD_95*2.20371 if country=="nld" 
    replace `var'_pi_USD_95=`var'_pi_USD_95*200.482 if country=="prt" 
    replace `var'_pi_USD_95=`var'_pi_USD_95*40.3399 if country=="lux"

}

keep year country Hourly_pi_USD_95

*import it into the main structure
drop if Hourly_pi_USD_95 == .
gen iso_code = upper(country)
*More limited list of ISO alpha 3 codes for countries that we use | by hand based on https://www.iso.org/obp/ui/#iso:pub:PUB500001:en
mmerge iso_code using ${commondata_dir}/patstat_2018b/country_codes.dta, unmatched(master) umatch(iso) ukeep(ctry)
drop country _m iso_code
ren Hourly_pi_USD_95 minwMP_

reshape wide minwMP_, i( year) j(ctry) string

*labeling
labelingvars "minw"

compress
save ${dataset_dir}/indep_vars/minwages_wide_MANUF.dta, replace


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat